﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using Real.Silverlight.Controls;

namespace Real.Silverlight.TestApp
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        #region ctor
        public MainPage()
        {
            InitializeComponent();
            Init();
        }

        //init
        private void Init()
        {
            ObservableCollection<DataGridMainModel> items = new ObservableCollection<DataGridMainModel>();
            for (int i = 0; i < 100; i++)
            {
                DataGridMainModel m = new DataGridMainModel()
                {
                    Item1 = "Item " + i,
                    Item2 = "Item " + i,
                    Item3 = "Item " + i,

                    SubModel = new DataGridSubModel()
                    {
                        Item1 = "Item " + i,
                        Item2 = "Item " + i
                    }
                };
                m.SubModel.MainModel = m;

                items.Add(m);
            }

            InitialList = items;

            LoadedUp(InitialList);

            //set datacontext
            this.DataContext = this;

            //set eventhandelers
            this.firstDataGrid.DataGridHeaderClicked += new Controls.DataGridHeaderClickedEventHandler(firstDataGrid_DataGridHeaderClicked);
            this.secondDataGrid.DataGridHeaderClicked += new Controls.DataGridHeaderClickedEventHandler(secondDataGrid_DataGridHeaderClicked);
        }
        #endregion

        #region eventhandlers
        void secondDataGrid_DataGridHeaderClicked(object sender, Controls.DataGridHeaderClickedEventArgs e)
        {
            var result = from i in InitialList
                         select i.SubModel;

            var r = IsSortAsc ? result.OrderBy(e.BindingPropertyName).ToList() :
                result.OrderByDescending(e.BindingPropertyName).ToList();


            //refresh lists (binded properties)

            RefreshSortedByList2(r);

            IsSortAsc = !IsSortAsc;
        }

        void firstDataGrid_DataGridHeaderClicked(object sender, Controls.DataGridHeaderClickedEventArgs e)
        {
            InitialList = IsSortAsc ? InitialList.OrderBy(e.BindingPropertyName) :
            InitialList.OrderByDescending(e.BindingPropertyName);

            IsSortAsc = !IsSortAsc;

            LoadedUp(InitialList);
        }

        #endregion

        #region properties
        private PagedCollectionView _ItemsList1;
        public PagedCollectionView ItemsList1
        {
            get { return _ItemsList1; }
            set
            {
                _ItemsList1 = value;
                RaisePropertyChanged("ItemsList1");
            }
        }

        private PagedCollectionView _ItemsList2;
        public PagedCollectionView ItemsList2
        {
            get { return _ItemsList2; }
            set
            {
                _ItemsList2 = value;
                RaisePropertyChanged("ItemsList2");
            }
        }

        private IEnumerable<DataGridMainModel> InitialList
        {
            get;
            set;
        }

        private DateTime? _TestDate;
        public DateTime? TestDate
        {
            get
            {
                return _TestDate;
            }
            set
            {
                _TestDate = value;
                RaisePropertyChanged("TestDate");
            }
        }


        private bool IsSortAsc { get; set; }
        #endregion

        #region methods

        private void LoadedUp(IEnumerable<DataGridMainModel> itemsFromServer)
        {
            ItemsList1 = new PagedCollectionView(itemsFromServer);
            var result = from i in itemsFromServer
                         select i.SubModel;
            ItemsList2 = new PagedCollectionView(result.AsEnumerable());
        }

        private void RefreshSortedByList2(IEnumerable<DataGridSubModel> items)
        {

            var result = from i in items
                         select i.MainModel;
            var r = result.ToList();

            ItemsList1 = new PagedCollectionView(r);
            ItemsList2 = new PagedCollectionView(items);

        }
        #endregion

        #region INotifyPropertyChanged - members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
